package com.itheima.Interceptor;

import com.itheima.Utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtUtils jwtUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader("token");
        if(header==null){
            response.setStatus(401);
            return false;
        }
        try {
            Map<String, Object> claims = jwtUtils.parseJwt(header);
            return true;
        } catch (Exception e) {
            response.setStatus(401);
            return false;
        }
    }
}
